package cn.sh.library.mgts.base;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
 * 汉字转换成拼音方法
 * @author 蔡龙
 */
 
public class Pinyin4j {
	
	HanyuPinyinOutputFormat format = null;  
	public static enum Type {  
		UPPERCASE,			  //全部大写  
		LOWERCASE,			  //全部小写  
		FIRSTUPPER            //首字母大写
	}  
  
	public Pinyin4j(){  
		format = new HanyuPinyinOutputFormat();  
		format.setCaseType(HanyuPinyinCaseType.UPPERCASE);  
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
	}  
  
	/**
	 * 转换全部大写 
	 * @param str 字符串
	 * @return str为颐和园 ,return获取到的是YHY
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinUppercase(String str) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, "", Type.UPPERCASE);  
	}  
  
	/**
	 * 转换全部大写
	 * @param str 字符串
	 * @param spera 转换字母间隔加的字符串,如果不需要为""
	 * @return str为颐和园 ,spera为** return获取到的是Y**H**Y
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinUppercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, spera, Type.UPPERCASE);  
	} 
	
	public String toPinYinshouzimu(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
		return spell(str);  
	} 
	
	
	
	/**
	 * 转换全部小写
	 * @param str 字符串
	 * @return	str为颐和园 ,return获取到的是yhy
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinLowercase(String str) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, "", Type.LOWERCASE);  
	}  
  
	/**
	 * 转换全部小写
	 * @param str 字符串
	 * @param spera 转换字母间隔加的字符串,如果不需要为""
	 * @return	str为颐和园 ,spera为** return获取到的是y**h**y
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinLowercase(String str,String spera) throws BadHanyuPinyinOutputFormatCombination{  
		return toPinYin(str, spera, Type.LOWERCASE);  
	} 
	
	/** 
	 * 获取拼音首字母(大写)
	 * @param str 字符串
	 * @return str为颐和园 ,return获取到的是Y
	 * @throws BadHanyuPinyinOutputFormatCombination 异常信息
	 */  
	public String toPinYinUppercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {  
		String initials = null;
		String py = toPinYinUppercase(str); 
		if(py.length()>1){
			initials = py.substring(0, 1);
		}
		if(py.length()<=1){
			initials = py;
		}
		return initials.trim();  
	} 
	
	/** 
	 * 获取拼音首字母(小写)
	 * @param str 字符串
	 * @return str为颐和园 ,return获取到的是y
	 * @throws BadHanyuPinyinOutputFormatCombination 异常信息
	 */  
	public String toPinYinLowercaseInitials(String str) throws BadHanyuPinyinOutputFormatCombination {  
		String initials = null;
		String py = toPinYinLowercase(str); 
		if(py.length()>1){
			initials = py.substring(0, 1);
		}
		if(py.length()<=1){
			initials = py;
		}
		return initials.trim();  
	} 
  
	/** 
	 * 将str转换成拼音，如果不是汉字或者没有对应的拼音，则不作转换 
	 * @param str    字符串
	 * @param spera  默认,可为""
	 * @param type   转换格式
	 * @return 按照转换格式转换成字符串
	 * @throws BadHanyuPinyinOutputFormatCombination 异常信息 
	 */  
	public String toPinYin(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {  
		if(str == null || str.trim().length()==0) { 
			return ""; 
		}
		if(type == Type.UPPERCASE) { 
			format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		} else{  
			format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		}
		String py = "";  
		String temp = "";  
		String[] t;  
		for(int i=0;i<str.length();i++){  
			char c = str.charAt(i);  
			if((int)c <= 128)  {
				py += c; 
			}else{  
				t = PinyinHelper.toHanyuPinyinStringArray(c, format);  
				if(t == null) { 
					py += c;  
				}else{  
					temp = t[0];  
					if(type == Type.FIRSTUPPER) { 
						temp = t[0].toUpperCase().charAt(0)+temp.substring(1); 
					}
					if(temp.length()>=1){
					   	temp = temp.substring(0, 1);
					}
						py += temp+(i==str.length()-1?"":spera);  
					}  
				}  
			}  
			return py.trim();  
		}  
	
	/**
	 * 将str转换成拼音，如果不是汉字或者没有对应的拼音，则不作转换
	 * 如： 明天 转换成 MINGTIAN
	 * @param str
	 * @param spera
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public String toPinYinAll(String str, String spera, Type type) throws BadHanyuPinyinOutputFormatCombination {
		if(str == null || str.trim().length()==0)
			return "";
		if(type == Type.UPPERCASE)
			format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		else
			format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		
		String py = "";
		String temp = "";
		String[] t;
		for(int i=0;i<str.length();i++){
			char c = str.charAt(i);
			if((int)c <= 128)
				py += c;
			else{
				//不能转拼音的部分会报错
				try {
					t = PinyinHelper.toHanyuPinyinStringArray(c, format);
					if(t == null)
						py += c;
					else{
						temp = t[0];
						if(type == Type.FIRSTUPPER)
							temp = t[0].toUpperCase().charAt(0)+temp.substring(1);
						py += temp+(i==str.length()-1?"":spera);
					}
				} catch (Exception e) {
					continue;				
				}
			}
		}
		return py.trim();
	}

	
    public String spell(String chinese) {  
        if (chinese == null) {  
            return null;  
        }  
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();  
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写  
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不标声调  
        format.setVCharType(HanyuPinyinVCharType.WITH_V);// u:的声母替换为v  
        try {  
            StringBuilder sb = new StringBuilder();  
            for (int i = 0; i < chinese.length(); i++) { 

                String[] array = PinyinHelper.toHanyuPinyinStringArray(chinese  
                        .charAt(i), format);  //非汉字此处取值为空
                if (array == null || array.length == 0) {  
                    continue;  
                }  
                String s = array[0];// 不管多音字,只取第一个  
                char c = s.charAt(0);// 大写第一个字母  
//                String pinyin = String.valueOf(c).toUpperCase().concat(s  
//                        .substring(1));  
//                sb.append(pinyin+" ");
                String pinyin = String.valueOf(c).toLowerCase().concat(s  
                        .substring(1));  
                sb.append(pinyin+" ");  
            }  
            return sb.toString().trim();  
        } catch (BadHanyuPinyinOutputFormatCombination e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
    
    //转汉字字符串中除汉字以外存在其他字符 如英文字母、标点等
    public String spellNotAllChinese(String chinese) {  
        if (chinese == null) {  
            return null;  
        }  
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();  
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 小写  
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不标声调  
        format.setVCharType(HanyuPinyinVCharType.WITH_V);// u:的声母替换为v  
        try {  
            StringBuilder sb = new StringBuilder();  
            for (int i = 0; i < chinese.length(); i++) { 
            	//判断是否为非汉字
            	if((chinese.charAt(i) >= 0x4e00)&&(chinese.charAt(i) <= 0x9fbb)){
            		String[] array = PinyinHelper.toHanyuPinyinStringArray(chinese  
	                        .charAt(i), format);  
	                if (array == null || array.length == 0) {  
	                    continue;  
	                }  
	                String s = array[0];// 不管多音字,只取第一个  
	                char c = s.charAt(0);// 大写第一个字母  
	//                String pinyin = String.valueOf(c).toUpperCase().concat(s  
	//                        .substring(1));  
	//                sb.append(pinyin+" ");
	                String pinyin = String.valueOf(c).toLowerCase().concat(s  
	                        .substring(1));  
	                sb.append(pinyin+" ");  
            	}else {
            		sb.append(chinese.charAt(i)); 
            	}
            }  
            return sb.toString().trim();  
        } catch (BadHanyuPinyinOutputFormatCombination e) {  
            e.printStackTrace();  
        }  
        return null;  
    }
}
